/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import React, { useState, useEffect, ReactElement } from 'react';
import { useNavigate } from 'react-router-dom';
import { BreakpointManager } from '@hadss/web_adaptive_layout_ui';
import './PlaylistHeader.css';
import headerBackground from '../../../assets/playList/header_background.png';
import backIcon from '../../../assets/playList/small_back.png';
import playlistCover from '../../../assets/playList/cover.png';
import playIcon from '../../../assets/playList/play_icon.png';
import showAllIcon from '../../../assets/playList/show_all.png';
import loveCount from '../../../assets/playList/love_count.png';
import downloadCount from '../../../assets/playList/download_count.png';
import commentCount from '../../../assets/playList/commen_count.png';
import shareIcon from '../../../assets/playList/share.png';

const PlaylistHeader = ({ songsLength, topAvoidArea }): ReactElement => {
  const navigate = useNavigate();
  const [paddingHorizontal, setPaddingHorizontal] = useState(16);
  const [listHeaderImage, setListHeaderImage] = useState(16);

  const imageNumber = '3456万';
  const playlistTitle = '轻快旋律：这里是疲倦灵魂栖息地';
  const playlistDescription =
    '怀旧使我快乐，人们总是以为自己的生活很枯燥/乏味，那是因为他们没有回过头来聆听那些曾陪伴过我们青春的旋律';
  const fourImages = [loveCount, downloadCount, commentCount, shareIcon];

  useEffect(() => {
    const breakpointManager = new BreakpointManager();

    const unsubscribe = breakpointManager.subscribeToBreakpoint(() => {
      setPaddingHorizontal(
        breakpointManager.useBreakpointValue({
          sm: 16,
          md: 113,
        })
      );
      setListHeaderImage(
        breakpointManager.useBreakpointValue({
          sm: 16,
          md: 24,
        })
      );
    });

    return (): void => {
      if (unsubscribe) {
        unsubscribe();
      }
    };
  }, []);

  const goBack = (): void => {
    navigate(-1);
  };

  return (
    <div className="listHeaderContainer">
      <div
        className="header-background"
        style={{
          backgroundImage: `url(${headerBackground})`,
          paddingTop: `${topAvoidArea ? topAvoidArea : 8}px`,
        }}
      >
        <button className="back-button" onClick={goBack}>
          <img src={backIcon} alt="Back" className="back-icon" />
        </button>
        <div
          className="playlist-info"
          style={{
            marginLeft: `${paddingHorizontal}px`,
            marginRight: `${paddingHorizontal}px`,
            width: 'auto',
          }}
        >
          <div className="image-wrapper">
            <img
              src={playlistCover}
              alt="Playlist Cover"
              className="playlist-image"
            />
            <div className="image-overlay">
              <span className="image-number">{imageNumber}</span>
            </div>
          </div>
          <div className="playlist-text">
            <h2 className="text-title">{playlistTitle}</h2>
            <p className="text-description" title={playlistDescription}>
              {playlistDescription}
            </p>
            <p className="author">Christopher</p>
          </div>
        </div>

        <div
          className="images-row"
          style={{
            marginLeft: `${paddingHorizontal}px`,
            marginRight: `${paddingHorizontal}px`,
            width: 'auto',
          }}
        >
          {fourImages.map((image, index) => (
            <img
              key={index}
              src={image}
              alt={`Action ${index + 1}`}
              className="action-image"
            />
          ))}
        </div>
      </div>

      <div
        className="list-header"
        style={{
          marginLeft: `${listHeaderImage}px`,
          marginRight: `${listHeaderImage}px`,
          width: 'auto',
        }}
      >
        <img src={playIcon} alt="Play All" className="list-header-icon" />
        <span className="list-header-text">播放全部</span>
        <span className="list-header-text">{songsLength}</span>
        <div className="flex-style"></div>
        <img src={showAllIcon} alt="Show All" className="open-icon-small" />
      </div>
    </div>
  );
};

export default PlaylistHeader;
